package com.cmy.framework_network.environment

import android.os.Bundle
import android.widget.Toast
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import com.cmy.framework_network.R

/**
 * 记录网络信息状态，提供网络页面状态
 */
class MyPreferenceFragment :PreferenceFragmentCompat(), Preference.OnPreferenceChangeListener {
    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        addPreferencesFromResource(R.xml.environment_preference)
        findPreference<Preference>(EnvironmentActivity.NETWORK_ENVIRONMENT_PREF_KEY)!!.onPreferenceChangeListener = this
    }

    override fun onPreferenceChange(preference: Preference?, newValue: Any?): Boolean {
        if (!EnvironmentActivity.sCurrentNetworkEnvironment.equals(
                newValue.toString(),
                ignoreCase = true
            )
        ) {
            Toast.makeText(context, "您已经更改了网络环境，再您退出当前页面的时候APP将会重启切换环境！", Toast.LENGTH_SHORT).show()
        }
        return true
    }
}